package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckGroupService;
import com.itheima.health.service.CheckItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author spt
 * @description 检查项Controller
 **/
@RestController
@RequestMapping("/checkgroup")
@Slf4j
public class CheckGroupController {
    @Autowired
    private CheckGroupService checkGroupService;

    @PostMapping("/add")
    @Transactional
    public Result add(@RequestParam List<Integer> checkitemIds, @RequestBody CheckGroup checkGroup) {
        //添加检查组基本信息

        checkGroupService.add(checkGroup);
        //添加检查组与检查项的关联信息
        for (Integer checkitemId : checkitemIds) {
            //获取到的检查组id
            Integer checkGroupId = checkGroup.getId();

            checkGroupService.addCheckitemAndCheckgroupWithId(checkGroupId, checkitemId);

        }

        return new Result(true, MessageConst.ADD_CHECKGROUP_SUCCESS);
    }

    @GetMapping("/findPage")
    public Result findPage(QueryPageBean queryPageBean) {
        log.info("分页查询的条件:{}", queryPageBean);
        PageResult pageResult = checkGroupService.queryPage(queryPageBean);
        return new Result(true, MessageConst.QUERY_CHECKGROUP_SUCCESS, pageResult);

    }

    @GetMapping("/findCheckItemIdsByCheckGroupId")
    public Result findCheckItemIdsByCheckGroupId(Integer id) {
        log.info("需要修改的项目组id");
        List<Integer> list = checkGroupService.findCheckItemIdsByCheckGroupId(id);
        return new Result(true, MessageConst.QUERY_CHECKITEM_SUCCESS, list);
    }

    @GetMapping("/findById")
    public Result findByid(Integer id) {
        CheckGroup checkGroup = checkGroupService.findByid(id);
        return new Result(true, MessageConst.QUERY_CHECKGROUP_SUCCESS, checkGroup);
    }

    @PostMapping("/edit")
    @Transactional
    public Result edit(@RequestParam("checkitemIds") List<Integer> checkitemIds, @RequestBody CheckGroup checkGroup) {
        //基本信息更新
        checkGroupService.updateEdit(checkGroup);
        //中间表 关联全部删除  删除中间表 获取checkGroup的id
        Integer checkGroupId = checkGroup.getId();
        checkGroupService.deleteById(checkGroupId);
        //重新添加关联 到中间表
        for (Integer checkitemId : checkitemIds) {
            //获取到的检查组id
            Integer checkgroupId = checkGroup.getId();
            checkGroupService.addCheckitemAndCheckgroupWithId(checkgroupId, checkitemId);
        }

        return new Result(true, MessageConst.EDIT_CHECKGROUP_SUCCESS);
    }

    @PostMapping("/deleteCheckGroupitemById")
    public Result deleteCheckGroupitemById(Integer id) {
        //先根据id删除中间关系表(因主外键关系无法直接删除)(因更新时就写过删除中间表方法 所以直接复用即可)
        checkGroupService.deleteById(id);
        //删除检查组信息
        checkGroupService.deleteCheckGroupitemById(id);
        return new Result(true, MessageConst.DELETE_CHECKGROUP_SUCCESS);
    }

    @GetMapping("findAll")
    public Result findAll(){
        List<CheckGroup> list = checkGroupService.findAll();
        return new Result(true,MessageConst.QUERY_CHECKITEM_SUCCESS,list);


    }

}
